﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2013
 * 
 * 文件名：DataAccessProduct.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：ifunnyface@outlook.com
 * 日 期：2013-01-07 14:44:10
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */


using DomainModel;
using IDataAccess;
using Infrastructure;
namespace DataAccess
{

    /// <summary>
    /// 
    /// </summary>
    public class DataAccessProduct : RepositoryBase<ProductInfo>, IDataAccessProduct
    {
        /// <summary>
        /// 
        /// </summary>
        public DataAccessProduct(IDbContextFactory dbContextFactory, IUnitOfWork unitOfWork)
            : base(dbContextFactory, unitOfWork)
        {
        }


        ///// <summary>
        ///// 通过Id获取分类
        ///// </summary>
        ///// <param name="partitionId">Id</param>
        ///// <returns>分类信息</returns>
        //public ProductPartitionInfo GetPartition(string partitionId)
        //{
        //    using (var db = new EFDBContext())
        //    {
        //        return db.ProductPartitions.Find(partitionId);
        //    }
        //}


        ///// <summary>
        ///// 获取最大编号的分类信息
        ///// </summary>
        ///// <param name="prefix">前缀</param>
        ///// <returns>分类信息</returns>
        //public string Max(string prefix)
        //{
        //    using (var db = new EFDBContext())
        //    {
        //        return
        //            db.ProductPartitions.Where(u => u.PartitionId.StartsWith(prefix))
        //              .Max(u => u.PartitionId.Substring(prefix.Length));
        //    }
        //}


        ///// <summary>
        ///// 获取分类
        ///// </summary>
        ///// <returns>分类列表</returns>
        //public IList<ProductPartitionInfo> GetPartitions()
        //{
        //    using (var db = new EFDBContext())
        //    {
        //        return db.ProductPartitions.ToList();
        //    }
        //}


        ///// <summary>
        ///// 添加分类信息
        ///// </summary>
        ///// <param name="partitionInfo">分类信息</param>
        ///// <returns>True=操作成功/False=操作失败</returns>
        //public bool AddPartition(ProductPartitionInfo partitionInfo)
        //{
        //    var result = 0;
        //    using (var db = new EFDBContext())
        //    {
        //        var model = db.ProductPartitions.SingleOrDefault(c => c.Name == partitionInfo.Name);
        //        if (null == model)
        //        {
        //            db.ProductPartitions.Add(partitionInfo);
        //            result = db.SaveChanges();
        //        }
        //    }
        //    return result > 0;
        //}


        ///// <summary>
        ///// 删除分类信息
        ///// </summary>
        ///// <param name="partitionId">Id</param>
        ///// <returns>True=操作成功/False=操作失败</returns>
        //public bool DeletePartition(string partitionId)
        //{
        //    if (string.IsNullOrEmpty(partitionId))
        //    {
        //        throw new ArgumentException("无效参数", "partitionId");
        //    }
        //    var result = 0;
        //    using (var db = new EFDBContext())
        //    {
        //        var model = db.ProductPartitions.SingleOrDefault(c => c.PartitionId == partitionId);
        //        db.ProductPartitions.Remove(model);
        //        result = db.SaveChanges();
        //    }
        //    return result > 0;
        //}



        ///// <summary>
        ///// 更新分类信息
        ///// </summary>
        ///// <param name="partitionInfo">分类信息</param>
        ///// <returns>True=操作成功/False=操作失败</returns>
        //public bool UpdatePartition(ProductPartitionInfo partitionInfo)
        //{
        //    using (var db = new EFDBContext())
        //    {
        //        var model = db.ProductPartitions.Find(partitionInfo.PartitionId);
        //        var to = ObjectMapperManager
        //                    .DefaultInstance
        //                    .GetMapper<ProductPartitionInfo, ProductPartitionInfo>()
        //                    .Map(partitionInfo, model);
        //        db.SaveChanges();
        //    }
        //    return true;
        //}


        ///// <summary>
        ///// 根据条件分页获得记录
        ///// </summary>
        ///// <param name="where">条件</param>
        ///// <param name="pageIndex">当前页码</param>
        ///// <param name="pageSize">每页大小</param>
        ///// <param name="orderByExpressions">排序字段</param>
        ///// <returns>记录列表</returns>
        //public IList<ProductPartitionInfo> LoadAll(Expression<Func<ProductPartitionInfo, bool>> where,
        //                         int pageIndex,
        //                         int pageSize,
        //                         params IOrderByExpression<ProductPartitionInfo>[] orderByExpressions)
        //{
        //    using (var db = new EFDBContext())
        //    {
        //        var list = db.ProductPartitions.Where(where);
        //        var totalRecord = list.Count();
        //        if (totalRecord <= 0)
        //        {
        //            return new List<ProductPartitionInfo>();
        //        }

        //        if (orderByExpressions != null && orderByExpressions.Any())
        //        {
        //            list = list.OrderBy(orderByExpressions);
        //        }
        //        else
        //        {
        //            list = list.OrderBy(new OrderByExpression<ProductPartitionInfo, int>(a => a.SortNumber));
        //        }
        //        list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);

        //        //调试输出SQL，注意query不能ToList()，否则就不是IQueryable了 
        //        Debug.WriteLine(list.ToString());

        //        return list.ToList();
        //    }
        //}


        ///// <summary>
        ///// 获取总数
        ///// </summary>
        ///// <param name="where">条件</param>
        ///// <returns>记录总数</returns>
        //public int LoadCount(Expression<Func<ProductPartitionInfo, bool>> where)
        //{
        //    var count = 0;
        //    using (var db = new EFDBContext())
        //    {
        //        count = db.ProductPartitions.Where(where).Count();
        //    }
        //    return count;
        //}
    }

}